import { _decorator, Color, Component, Label, Node, Sprite, Vec3 } from 'cc';
import EvtManger from '../tools/EvtManger';
import { EventName, gunResolve_config, prop_config, secondaryGun_config } from '../manager/AppControll';
import { GameData, SecondaryGunStruct } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import { StuffIcon } from './StuffIcon';
import { Loader } from '../tools/LoadManger';
const { ccclass, property } = _decorator;

@ccclass('secondGunChoose')
export class SecondGunChoose extends Component {
    private mNdCur: Node = null;
    private mNdNew: Node = null;
    private mNUnlockId: number = null;
    private mCurGunSct: SecondaryGunStruct = null;
    private mNewGunSct: SecondaryGunStruct = null;
    private mCurPorp: gunResolve_config = null;
    private mNewProp: gunResolve_config = null;

    InitMember() {
        let body = this.node.getChildByName("body");
        this.mNdCur = body.getChildByName("curGun");
        this.mNdNew = body.getChildByName("newGun");
    }

    InitChoose(curSct: SecondaryGunStruct, AlSct: SecondaryGunStruct, id: number) {
        this.InitMember();
        this.mCurGunSct = curSct;
        this.mNewGunSct = AlSct;
        this.mNUnlockId = id;
        let curConfig = DataHelper.GetSpecialSkillConfigById(curSct.configId);
        this.mCurPorp = DataHelper.GetGunResolveConfigByQuality(curConfig.skillQuality);
        let newConfig = DataHelper.GetSpecialSkillConfigById(AlSct.configId);
        this.mNewProp = DataHelper.GetGunResolveConfigByQuality(newConfig.skillQuality);
        this.InitView();
    }

    InitView() {
        this.InitGunNd(this.mNdCur, this.mCurGunSct, this.mNewGunSct);
        this.InitGunNd(this.mNdNew, this.mNewGunSct, this.mCurGunSct);
    }

    InitGunNd(node: Node, sct: SecondaryGunStruct, asct: SecondaryGunStruct) {
        let config = DataHelper.GetSpecialSkillConfigById(sct.configId);
        let aConfig = DataHelper.GetSpecialSkillConfigById(asct.configId);
        let configlv: secondaryGun_config = DataHelper.GetSecondaryConfigByLv(sct.level);
        let propConifg = DataHelper.GetGunResolveConfigByQuality(config.skillQuality);
        let apropConfig = DataHelper.GetGunResolveConfigByQuality(aConfig.skillQuality);
        let ndStart = node.getChildByName("star").getChildByName("starHave");
        let lbAtk = node.getChildByName("attribute").getChildByName("atk").getChildByName("atkNum").getComponent(Label);
        let lbSpeed = node.getChildByName("attribute").getChildByName("hitSpeed").getChildByName("atkNum").getComponent(Label);
        let lbDescription = node.getChildByName("attribute").getChildByName("hitSpeed").getChildByName("dec").getComponent(Label);
        let ndResreveGet = node.getChildByName("get").getChildByName("icon");
        let lbResreveGet = node.getChildByName("get").getChildByName("num").getComponent(Label);
        node.getChildByName("gunLevel").getComponent(Label).string = sct.level + "";
        node.getChildByName("gunName").getComponent(Label).string = config.secondGunName;
        node.getChildByName("gunName").getComponent(Label).color = new Color(DataHelper.GetColorByLv(config.skillQuality + 1).value);
        lbAtk.string = (configlv.aggressivity * DataHelper.GetGunAtkProportionByQuality(config.skillQuality)).toFixed(2);
        lbSpeed.string = configlv.attackSpeed.toFixed(2);
        lbDescription.string = config.skillDescribe;
        if (!ndResreveGet.getComponent(StuffIcon)) {
            ndResreveGet.addComponent(StuffIcon);
        }
        ndResreveGet.getComponent(StuffIcon).ShowStuff(apropConfig.resolveGet[0]);
        lbResreveGet.string = apropConfig.resolveGet[1] + "";
        let count = 0;
        Loader.LoadImage("image/gun/secondaryGun/" + config.secondGunRes, (spf) => {
            node.getChildByName("gunIcon").getComponent(Sprite).spriteFrame = spf;
        })
        ndStart.children.forEach(elemt => {
            if (config.skillQuality >= count) {
                elemt.active = true;
            } else {
                elemt.active = false;
            }
            count++;
        })
    }

    OnBtnRetain() {
        let iconPath = GameData.mRes[this.mNewProp.resolveGet[0]];
        let propConig = { propIcon: iconPath }
        EvtManger.do(EventName.dropMaterial, propConig, Vec3.ZERO);
        EvtManger.do(EventName.resetGun, this.mNUnlockId, this.mCurGunSct);
        GameData.GetNumByType(this.mNewProp.resolveGet[0], this.mNewProp.resolveGet[1])
        this.node.destroy();
    }

    OnBtnReplace() {
        let iconPath = GameData.mRes[this.mCurPorp.resolveGet[0]];
        let propConig = { propIcon: iconPath }
        EvtManger.do(EventName.dropMaterial, propConig, Vec3.ZERO);
        EvtManger.do(EventName.resetGun, this.mNUnlockId, this.mNewGunSct);
        GameData.GetNumByType(this.mCurPorp.resolveGet[0], this.mCurPorp.resolveGet[1])
        this.node.destroy();
    }
}


